package com.ray.auth.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.DictService;
import com.ray.auth.system.service.WorkService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.auth.system.table.entity.Work;
import com.ray.common.util.DataGridUtil;
import com.ray.common.util.DictLoadUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;


@Controller
@RequestMapping("/system/work")
public class WorkController extends WebController{

	@Autowired
	private ButtonService buttonService;
	@Autowired
	private WorkService workService;
	@Autowired
	private DictService dictService;
	@Autowired 
	private DictLoadUtil DictLoadUtil;
	
	@RequestMapping(value="view")
	@RequiresPermissions("work:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		  LoginUser user = getLoginUser();
		  Map<String,List<Button>> buttons=buttonService.queryUserButtonsWithMenu(user, menuId);
		  modelAndView.addObject("buttons",buttons);
		  modelAndView.setViewName("system/work/view");
		  return modelAndView;
		
	}
	/**
	 * 新增页面
	 * @param menuId
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value="workAddView")
	@RequiresPermissions("work:add")
	public ModelAndView addView(String menuId,Work params,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
		Map<String,List<Button>> buttons=buttonService.queryUserButtonsWithMenu(user, menuId);
		
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","workType");
	
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("buttons",buttons);
		modelAndView.addObject("params",params);
		modelAndView.setViewName("system/work/add");
		return modelAndView;
	}
	
	/**
	 * 用户新增
	 */
	@RequestMapping(value = "workAdd")
	@RequiresPermissions("work:add")
	@ResponseBody
	public ResponseEntity<Result<Object>> add(Work params,HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		Result<Object> result=workService.addWork(params,user);
		return doResult(result);
	}
	/*
	 * 用户修改页面
	 */
	@RequestMapping(value = "workUpdateView")
	@RequiresPermissions("work:edit")
	@ResponseBody
	public ModelAndView updateView(String menuId,String id,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user= getLoginUser();
		Map<String,List<Button>> buttons=buttonService.queryUserButtonsWithMenu(user, menuId);
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","workType");
		Work params=workService.queryWork(id,user);
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("buttons", buttons);
		modelAndView.addObject("params",params );
		modelAndView.setViewName("system/work/update");
		return modelAndView;
		   
	}
	
	/**
	 * 用户修改
	 */
	@RequestMapping(value = "workUpdate")
	@RequiresPermissions("work:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> update(Work params,HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		Result<Object> result=workService.updateWork(params,user);
		return doResult(result);
		
	}
	
	/**
	 * 用户详情页面
	 */
	@RequestMapping(value = "workDetailView")
	@RequiresPermissions("work:detail")
	@ResponseBody
	public ModelAndView detailView(String menuId,String id,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user= getLoginUser();
		Map<String,List<Button>> buttons=buttonService.queryUserButtonsWithMenu(user, menuId);
		Work params=workService.queryWork(id,user);
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","workType");
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("buttons", buttons);
		modelAndView.addObject("params",params );
		modelAndView.setViewName("system/work/detail");
		return modelAndView;
		   
	}
	/**
	 * 用户删除页面
	 */
	@RequestMapping(value = "workDelete")
	@RequiresPermissions("work:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> delete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser user =getLoginUser();
		Result<Object> result=workService.deleteWork(id,user);
		return doSuccess(result);
		
	}
	/**
	 * 用户查询页面
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("work:query")
	@ResponseBody
    public ResponseEntity<Map<String,Object>> query(Work params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		PageDto<Work> page=this.createJQPage(request);
		page=workService.queryWork(params,page,user);
		return doSuccess(DataGridUtil.dataConvert(page));

	}	
}
